/**
 * 作者：陈文斌 on 2019/1/17.
 * 邮箱：chen_wennbin@163.com
 * 版本：v1.0
 */

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:crm/model/main_scoped_model.dart';
import 'package:crm/model/Company/company_info_model.dart';
import 'package:crm/component/ClientManager/ImpoetClient/import_client_list_row.dart';
import 'package:crm/viewModel/ClientManager/client_manager_view_model.dart';
import 'package:crm/view/Refresh/refresh_footer_normal.dart';

class ImportClientListSection extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ImportClientListSectionState();
  }
}

class _ImportClientListSectionState extends State<ImportClientListSection> {
  ScrollController _listController = ScrollController();
  List<CompanyInfoModel> _companies = List<CompanyInfoModel>();
  int _selCompanyIndex = 0;

  bool _isPerformingRequest = false;
  int _page = 0;
  bool _isMore = true;

  _get(page) async {
    if (!_isPerformingRequest) {
      _isPerformingRequest = true;
      ClientManagerVM().getCompany(
        page,
        _companies,
        callback: (companies, bool isMore) {
          // 请求成功
          _isMore = isMore;
          _isPerformingRequest = false;
          _companies = companies;

          if (!mounted) return;
          setState(() {});
        },
        errorCallback: (errMsg) => print(errMsg),
      );
    }
  }

  @override
  void initState() {
    super.initState();
    _get(_page);
    _listController.addListener(() {
      if (_listController.position.pixels ==
          _listController.position.maxScrollExtent) _get(++_page);
    });
  }

  @override
  void dispose() {
    super.dispose();
    _listController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: RefreshIndicator(
        displacement: 20.0,
        child: createListView(context),
        onRefresh: () async {
          _page = 0;
          _get(_page);
        },
      ),
    );
  }

  Widget createListView(BuildContext context) {
    MainModel importClientSetSM = MainModel().of(context);
    return ListView.builder(
      controller: _listController,
      physics: const AlwaysScrollableScrollPhysics(),
      itemCount: _companies.length + 1,
      itemBuilder: (context, i) {
        if (i < _companies.length) {
          CompanyInfoModel _company = _companies[i];
          bool isSelect = _selCompanyIndex == i;
          return ImportClientListRow(
            onTap: () {
              importClientSetSM.client.companyName = _company.Name ?? '';
              importClientSetSM.client.industryClassification = _company.CustomIndustryCategories ?? '';
              importClientSetSM.client.phone = _company.ServiceTel ?? '';
              importClientSetSM.client.location = json.encode([_company.ResidentProvince ?? '',_company.ResidentCity ?? '',_company.ResidentArea ?? '']);
              importClientSetSM.client.streetaddress = _company.Address ?? '';
              _selCompanyIndex = i;
              setState(() {});
            },
            client: _company,
            isSelect: isSelect,
          );
        } else {
          return RefreshFooterNormal(isMore: _isMore);
        }
      },
    );
  }
}
